package net
{
	import Base.Net.NetSocket;
	
	import Common.GameDefine;
	import Common.GameNetEvent;
	import Common.GameNetManager;
	
	import flash.utils.ByteArray;
	
	import ui.ChatUIHandler;
	import ui.NpcTalkUIHandler;

	public class NpcNetHandler
	{
		public function NpcNetHandler()
		{
			
		}
		
		
		public static function onRecvNpcMove( data:ByteArray ):void
		{
			var id:int = data.readInt();

			var npc:GameNPC = GameCreatureManager.instance.getNpc( id );
			npc.data.Fall = data.readInt() == 1;
			npc.setPos( data.readShort() , data.readShort() );
		}
		
		public static function onRecvTalkNpc( data:ByteArray ):void
		{
			var id:int = data.readInt();
			var type:int = data.readInt();
			var mode:int = data.readInt();
			var talk:String = data.readUTFBytes( data.bytesAvailable );
			
			var npc:GameNPC = GameCreatureManager.instance.getNpc( id );
			
			
			NpcTalkUIHandler.instance.id = id;
			NpcTalkUIHandler.instance.type = type;
			NpcTalkUIHandler.instance.mode = mode;
			if ( mode != GameDefine.INVALIDID )
			{
				NpcTalkUIHandler.instance.title = npc.Name;
				NpcTalkUIHandler.instance.talk = talk;
				NpcTalkUIHandler.instance.setText();
				NpcTalkUIHandler.instance.visible( true );
			}
			else
			{
				ChatUIHandler.instance.addText( npc.Name + ":" + talk , false );
			}
		}
		
		
		public static function sendTalkNpc( id:int , state:int , select:int ):void
		{
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;
			
			netSocket.writeShort( GameNetEvent.SEND_TALK_NPC_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_TALK_NPC_ID );
			
			netSocket.writeInt( id );
			netSocket.writeInt( state );
			netSocket.writeInt( select );
			
			netSocket.Send();
		}
		
		
		
	}
}